package com.jianganming.template.jdbc.framework;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//模版方法设计模式
public class Test {
    private static JdbcTemplate jdbcTemplate = JdbcConfig.getInstance().getJdbcTemplate();

    public static void main(String[] args) throws Exception {
        selectBrand();

        selectCategory();


    }

    private static void selectCategory()throws Exception{
        String sql = "select * from tb_category where id > ? and id <?";
        List<Map<String, Object>> objects = jdbcTemplate.executeQuery(sql, (rs, row) -> {
            Map<String, Object> map = new HashMap<>();
            map.put("name", rs.getString("name"));
            return map;
        }, new Object[]{992,999});
        //System.out.println(objects.size());
        for (Map object : objects) {
            System.out.println(object);
        }
    }

    private static void selectBrand() throws Exception{
        String sql = "select * from tb_brand";
        Object[] objects = new Object[]{};
        List<Brand> list = jdbcTemplate.executeQuery(sql, new BrandRowMapper(), objects);
        for (Brand o : list) {
            System.out.println(o);
        }
    }




}

class BrandRowMapper implements RowMapper<Brand>{

    @Override
    public Brand mapRow(ResultSet rs, int rowNum) throws Exception {
        Brand brand = new Brand();
        brand.setId(rs.getInt("id"));
        brand.setName(rs.getString("name"));
        brand.setImage(rs.getString("image"));
        brand.setLetter(rs.getString("letter"));
        return brand;
    }
}

class Brand{
    private Integer id;
    private String name;
    private String image;
    private String letter;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getLetter() {
        return letter;
    }

    public void setLetter(String letter) {
        this.letter = letter;
    }

    @Override
    public String toString() {
        return "Brand{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", image='" + image + '\'' +
                ", letter='" + letter + '\'' +
                '}';
    }
}
